import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getArticleList,
  aticleStatus,
  updateData,
  addData,
  aticleSort,
  editSort,
  addSort,
  editArticleList,
  removeSort
} from '@/api/article'
import LocalCache from '@/utils/cache'

export default {
  namespaced: true,
  state: () => ({
    articleList: [],
    articleTotal: 0,
    sortData: [],
    sortTotal: 0
  }),
  mutations: {
    getArticleListMutations(state, playLoad) {
      state.articleList = playLoad.list
      state.articleTotal = playLoad.total
    },
    getSortListCommit(state, playLoad) {
      state.sortData = playLoad
    }
  },
  actions: {
    async getArticleListAction({ commit }, info) {
      const res = await getArticleList(info)
      commit('getArticleListMutations', { ...res })
    },
    async switchChangeActions({ commit, dispatch }, info) {
      const res = await editArticleList({
        id: info.id,
        status: info.status
      })
      if (!res.info) {
        return ElMessage.error(res.msg)
      }
      ElMessage.success('操作成功')
      dispatch('getArticleListAction', {
        status: '',
        category_id: '',
        page: 1,
        pageSize: 10,
        keywords: ''
      })
    },
    async submitActions({ commit, dispatch }, info) {
      const res = await updateData(info)
      if (!res.info) {
        return ElMessage.error(res.msg)
      }
      ElMessage({
        message: '修改成功！',
        type: 'success'
      })
      // 调用刷新表格
      dispatch('getArticleListAction', {
        status: '',
        category_id: '',
        page: 1,
        pageSize: 10,
        keywords: ''
      })
    },
    async searchArticleActions({ commit, dispatch }, info) {
      dispatch('getArticleListAction', info)
    },
    async addSubmitActions({ commit, dispatch }, info) {
      const res = await addData(info)
      if (res.info == '') {
        ElMessage({
          message: '修改成功！',
          type: 'success'
        })
        return dispatch('getArticleListAction', {
          status: '',
          category_id: '',
          page: 1,
          pageSize: 10,
          keywords: ''
        })
      }
      ElMessage.error(res.msg)
    },
    //
    async getSortListActions({ commit }, info) {
      const res = await aticleSort()
      commit('getSortListCommit', res)
    },
    async serachDataActions({ commit, dispatch }, playLoad) {
      console.log(playLoad)
    },
    async editDataActions({ commit, dispatch }, info) {
      const res = await editSort({
        content: info.content,
        id: info.id
      })
      if (!res.info) {
        return ElMessage.error(res.msg)
      }
      ElMessage({
        message: '修改成功！',
        type: 'success'
      })
      // 调用刷新列表
      dispatch('getSortListActions')
    },
    async sortAddSubmitActions({ commit, dispatch }, playLoad) {
      const res = addSort(playLoad)
      ElMessage({
        message: '修改成功！',
        type: 'success'
      })
      dispatch('getSortListActions')
    },
    async removeSortActions({ commit, dispatch }, playLoad) {
      const res = await removeSort({ id: playLoad.id })
      ElMessage.success('成功删除！')
      dispatch('getSortListActions')
    }
  }
}
